<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      delete-button-text="重置"
      :address-info="info"
    >
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label">
          <span>标签</span>
        </div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label" direction="horizontal">
              <van-radio name="家">家</van-radio>
              <van-radio name="公司">公司</van-radio>
              <van-radio name="学校">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { reqAddressAdd, reqAddressEdit, reqAddressList } from "../../http/http";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList: areaList,
      label: "label",
      info: {
        name: "",
        tel: "",
        province: "",
        city: "",
        county: "",
        addressDetail: "",
        areaCode: "",
        isDefault: false,
      },
    };
  },
  activated() {
    // 重置 info
    this.info = {
      name: "",
      tel: "",
      province: "",
      city: "",
      county: "",
      addressDetail: "",
      areaCode: "",
      isDefault: false,
    };
    this.label = "";
    // 如果获取到id 代表修改地址
    if(this.$route.query.id) {
      reqAddressList({
        id: this.$route.query.id,
        uid: JSON.parse(localStorage.getItem("userInfo")).uid,
      }).then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          let massage = res.data.list[0];
          this.info = {
            id: massage.id,
            name: massage.username,
            tel: massage.userphone,
            addressDetail: massage.useraddress,
            areaCode: massage.regioncode,
            isDefault: massage.status == 1,
          };
          this.label = massage.label;
        }
      });
    }
  },
  methods: {
    onSave(e) {
      if(this.$route.query.id) {
        reqAddressEdit({
          id: this.$route.query.id,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code === 200) {
            console.log(res);
            Toast(res.data.msg);
            this.$router.replace("/address");
          }
        });
      } else {
        reqAddressAdd({
          uid: JSON.parse(localStorage.getItem("userInfo")).uid,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode, //地区码
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          staus: e.isDefault ? 1 : 0,
          lable: this.label,
        }).then((res) => {
          // console.log(res);
          if (res.data.code === 200) {
            Toast(res.data.msg);
            this.$router.replace("/address");
          }
        });
      }
    },
  },
};
</script>
  
<style>
</style>